<?php
/*
 * @Author: LiuHaiL 18655112670@139.com
 * @Date: 2022-08-10 17:12:38
 * @LastEditors: LiuHaiL 18655112670@139.com
 * @LastEditTime: 2022-11-23 17:11:28
 * @FilePath: \ant_form_admind:\laragon\www\ghpt_api\app\api\controller\v1\Test.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

declare(strict_types=1);

namespace app\api\controller\v1;

use app\api\model\v1\Learn;
use app\api\model\v1\LearnTopic;
use app\api\model\v1\TopicExerciseLog;
use app\api\model\v1\TopicUserDay as AppTopicUserDay;
use app\api\model\v1\Unit;
use app\BaseController;
use think\facade\Db as ThinkDb;
use think\Request;

//培训现场考试控制器
class Test extends BaseController
{

    public function getTestExam()
    {

        $map[] = array('learn_id', '=', 20);
        $data = LearnTopic::field('id,topic_title as title,learn_id,learn_answer as answer,question_type as type,answer_explain,score')->with(['optionList' => function ($query) {
            $query->field('id,option_title as content,learn_topic_id');
            return $query;
        }])->where($map)->select()->each(function ($item, $key) {
            $optionList_arr = $item['optionList']->toArray();
            if ($item['question_type'] !== TopicExerciseLog::TOPIC_FOUR && $item['question_type'] !== TopicExerciseLog::TOPIC_FIVE) {
                $change_option = TopicExerciseLog::changeOption(json_encode($optionList_arr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $optionList_arr[intval($item['answer']) - 1]['id'] . '', '');
                $item['new_answer'] = $change_option['new_answer']; //新正确答案
            }
            $item['userAnswer'] = "";
            $item['isAnswer'] = false;
            $item['isBtn'] = false; //是否点击了确定
            $item['collectionTopic'] === null ? $item['userFavor'] = false : $item['userFavor'] = true;
            return $item;
        })->toArray();
        return $this->response($data);
    }



    public function tj_test()
    {
        $this->redis->select(1);
        // $parent_key = 'unit:child:1:*';
        // $redis_value = $this->redis->keys($parent_key);
        // halt($redis_value);
        //新增父级节点到redis中
        // $parent_key = 'parent:';
        // $res = Unit::field('id,unit_title')->where('pid',230)->select()->toArray();
        // foreach($res as $key=>$value){
        //     $parent_key = 'parent:'.$value['id'];
        //     $is_key = $this->redis->has($parent_key);
        //     if(!$is_key){
        //           //新增到redis中
        //         $this->redis->zadd($parent_key, $value['id'], json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
        //     }
        // }

        // halt("新增redis成功");
        // //获取子级节点到redis中
        $res = Unit::where('pid', 230)->select()->toArray();
        foreach ($res as $key => $value) {
            //获取节点下的所有子节点
            $parent_key = 'unit:parent:' . $value['id'];
            $arr = [];
            $parent_tj_arr = $this->totalCount($value['id']);
            $value['total'] = $parent_tj_arr['total'];
            $value['ids'] = $parent_tj_arr['ids'];
            $child = $this->search->getChild($value['id']);
            //二级
            $arr_child = [];
            foreach ($child as $k1 => $v1) {
                $child_key = 'unit:child:' . $value['id'] . ':' . $v1['id'];
                $parent_child_arr = $this->totalCount($v1['id']);
                $v1['total'] = $parent_child_arr['total'];
                $v1['ids'] = $parent_child_arr['ids'];
                //三级
                $arr_child_child = [];
                $child_child = $this->search->getChild($v1['id']);
                foreach ($child_child as $k2 => $v2) {
                    $child_child_key = 'unit:child:child:' . $v1['id'] . ':' . $v2['id'];
                    $parent_child_child_arr = $this->totalCount($v2['id']);
                    $v2['total'] = $parent_child_child_arr['total'];
                    $v2['ids'] = $parent_child_child_arr['ids'];
                    //ZRANGE runoobkey 0 10 WITHSCORES
                    $redis_child_child_value = $this->redis->ZREVRANGEBYSCORE($child_child_key, $v1['id'], $v1['id']);
                    if (!empty($redis_child_child_value)) {
                        $arr_child_child = json_decode($redis_child_child_value[0], true);
                        if (!empty($arr_child_child)) {
                            $accoss_child_child = array_diff_assoc($v2, $arr_child_child);
                            if (!empty($accoss_child_child)) {
                                $this->redis->ZREM($child_child_key, json_encode($arr_child_child, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
                            }
                        }
                    }
                    $this->redis->zadd($child_child_key, $v2['id'], json_encode($v2, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
                }
                $redis_child_value = $this->redis->ZREVRANGEBYSCORE($child_key, $v1['id'], $v1['id']);
                if (!empty($redis_child_value)) {
                    $arr_child = json_decode($redis_child_value[0], true);
                    if (!empty($arr_child)) {
                        $accoss_child = array_diff_assoc($v1, $arr_child);
                        if (!empty($accoss_child)) {
                            $this->redis->ZREM($child_key, json_encode($arr_child, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
                        }
                    }
                }
                $this->redis->zadd($child_key, $v1['id'], json_encode($v1, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
            }
            //ZRANGE runoobkey 0 10 WITHSCORES
            $redis_value = $this->redis->ZREVRANGEBYSCORE($parent_key, $value['id'], $value['id']);
            if (!empty($redis_value)) {
                $arr = json_decode($redis_value[0], true);
            }
            if (!empty($arr)) {
                $accoss = array_diff_assoc($value, $arr);
                if (!empty($accoss)) {
                    $this->redis->ZREM($parent_key, json_encode($arr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
                }
            }
            $this->redis->zadd($parent_key, $value['id'], json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
        }
        halt(123);
    }



    public function totalCount($id)
    {
        $childCountArr = $this->search->getChildren($id);
        $new_arr = [];
        $ids_arr = [];
        foreach ($childCountArr as $k => $v) {
            if ($v['level'] != 2) {
                $new_arr[] = $v;
                $ids_arr[] = $v['id'];
            }
        }

        $total = count($new_arr);
        $ids = implode(',', $ids_arr);
        return [
            'total' => $total,
            'ids' => $ids
        ];
    }



    public function sjb()
    {
        $this->redis->select(1);
        // $value = $this->redis->ZRANGE('unit:parent:1', '0', '-1');
        // $arr = json_decode($value[0], true);


        // $arr = json_decode($value[0], true);
        // $data = $this->tj_cs($arr);
        // halt($v);
        // $arr['wcl'] = $data['wcl'];
        // $arr['zql'] = $data['zql'];

        // halt($correct/$zzz);
        $parent_key = 'unit:parent:*';
        $redis_value = $this->redis->keys($parent_key);
        $new_Arr = [];
        foreach ($redis_value as $key => $v) {
            $value = $this->redis->ZRANGE($v, '0', '-1');
            $arr = json_decode($value[0], true);
            // $data = $this->tj_cs($arr);
            // $arr['wcl'] = $data['wcl'];
            // $arr['zql'] = $data['zql'];
            $new_Arr[] = $arr;
        }
        halt($new_Arr);
    }


    public function tj_cs($arr)
    {
        $count = AppTopicUserDay::whereIn('unit_id', $arr['ids'])->where('date_of', '2022-11-15')->group('unit_id')->count();
        $subQuery = ThinkDb::table('topic_user_day')
            // ->whereDay('create_time', date("Y-m-d"))
            ->whereIn('unit_id', $arr['ids'])
            ->where('date_of', '2022-11-15')
            ->where('is_admin', 0) //不为管理员的时候
            ->order('error asc')
            ->buildSql();
        $res = ThinkDb::table($subQuery . ' a')
            ->group('a.user_id')
            ->order('a.id desc')
            ->select()->toArray();
        $correct = 0;
        $zzz = 0;
        foreach ($res as $key => $v) {
            $zzz += 10;
            $correct += $v['correct'];
        }
        $wcl = round(($count / $arr['total']) * 100, 2);
        $zql = round(($correct / $zzz) * 100, 2);
        return [
            'wcl' => $wcl,
            'zql' => $zql,
        ];
    }





    public  function la()
    {
        $abc = Unit::field('id,unit_title')->with(['users'=>function($q){
            $q->field('id,username,mobile,wx_role_id,unit_id')->where('wx_role_id','in','2,4');
            return $q;
        }])->where('pid',12)->select()->each(function($item,$key){
            $username = "";
            $mobile = "";
            foreach($item['users'] as $key=>$value){
                $username .= $value['username'].',';
                $mobile .= $value['mobile'].',';

            }
            $item['username'] = rtrim($username,',');
            $item['mobile'] = rtrim($mobile,',');
        })->toArray();

        $abdd = [];
        foreach($abc as $key=>$v){
            
            unset($v['users']);
            $abdd[$key] = $v['unit_title'].'-'.$v['username'].'-'.$v['mobile'];
        }


        halt($abdd);
    }
}



